package com.lzl.edu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzl.edu.mappers.UserMapper;
import com.lzl.edu.pojo.User;
import com.lzl.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>leezhongloong_ws</h3>
 * <p></p>
 *
 * @author : 黎钟龙
 * QQ:1661548820
 * Mail：1661548820@qq.com
 * @date : 2022-03-18 09:20
 **/

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public User queryById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateById(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
//    事务处理注解
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public void saveUser(User user) {
        userMapper.insertSelective(user);
    }

//    查询全部
    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<User> queryAll() {
        return userMapper.selectAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteById(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<User> pageInfo(Integer curPage, Integer pageSize) {
//        开启分页
        PageHelper.startPage(curPage,pageSize);
        List<User> users = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

}
